package com.example.testanimation.fragment.switchanimation;


import android.animation.*;
import android.annotation.TargetApi;
import android.os.Build;
import android.view.View;
import com.example.testanimation.objectanimation.ShapeHolder;

/**
 * Created by scenic on 3/26/15.
 */
public class ScaleViewAnimation {

    private final int originHeight;
    private final int originWidth;

    private int startWidth ;
    private int startHeight;
    View view;
    int startX;
    int startY;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    ScaleViewAnimation(View view, int startX, int startY, int width, int height) {
        this.view = view;
        this.startX = startX;
        this.startY = startY;
        this.startWidth = width;
        this.startHeight = height;

        view.setX(startX);
        view.setY(startY);
        originHeight = view.getHeight();
        originWidth = view.getWidth();
        view.getLayoutParams().width = width;
        view.getLayoutParams().height = height;
        ofObject = new WidthHeight(view);
    }

    WidthHeight ofObject ;

    public void start(Animator.AnimatorListener listener) {
        PropertyValuesHolder holderX = PropertyValuesHolder.ofFloat("X",startX,0);
        PropertyValuesHolder holderY = PropertyValuesHolder.ofFloat("Y",startY,0);
        PropertyValuesHolder holderWidth = PropertyValuesHolder.ofFloat("Width",startWidth,originWidth);
        PropertyValuesHolder holderHeight = PropertyValuesHolder.ofFloat("Height",startHeight,originHeight);
        ObjectAnimator value = ObjectAnimator.ofPropertyValuesHolder(ofObject, holderX, holderY, holderWidth, holderHeight).
                setDuration(1000);

        value.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                System.out.println(animation.getCurrentPlayTime());
            }
        });
        value.addListener(listener);
        value.start();

    }

//    public void execute(String proxy,WidthHeight start,WidthHeight end){
//        ObjectAnimator.ofObject(ofObject,proxy,new TypeEvaluator<WidthHeight>(){
//            @Override
//            public WidthHeight evaluate(float fraction, WidthHeight startValue, WidthHeight endValue) {
//                WidthHeight tmp = new WidthHeight(view);
//                tmp.x = startValue.x + (endValue.x - startValue.x) * fraction;
//                return ;
//            }
//        },start,end);
//    }


    class WidthHeight {
        View view;
        WidthHeight(View view){
            this.view = view;
        }

        float x;
        float y;
        float width;
        float height;

        public WidthHeight setWidth(float width) {
            this.width = width;
            view.getLayoutParams().width = (int) width;
            view.requestLayout();
            return this;
        }

        public WidthHeight setHeight(float height) {
            this.height = height;
            view.getLayoutParams().height = (int) height;
            view.requestLayout();
            return this;
        }

        public WidthHeight setX(float x){
            this.x = x;
            view.setX(x);

            return this;
        }

        public WidthHeight setY(float y){
            view.setY(y);
            this.y = y;
            return this;
        }
    }



}
